package com.liujianlei.forum_system.service.impl;

import com.liujianlei.forum_system.common.AppResult;
import com.liujianlei.forum_system.common.ResultCode;
import com.liujianlei.forum_system.dao.ArticleMapper;
import com.liujianlei.forum_system.dao.ArticleReplyMapper;
import com.liujianlei.forum_system.exception.ApplicationException;
import com.liujianlei.forum_system.model.Article;
import com.liujianlei.forum_system.model.ArticleReply;
import com.liujianlei.forum_system.service.IArticleReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tongchen
 * @create 2023-06-15 15:21
 */
@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {
    @Resource
   private ArticleMapper articleMapper;
    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Override
    @Transactional
    public void create(ArticleReply reply) {
        //判断回复对象是否有效
        if(reply==null||reply.getArticleId()==null){
            log.error("文章回复信息为空");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //判断对应的帖子信息是否有效
        Article article = articleMapper.selectByPrimaryKey(reply.getArticleId());
        if(article==null||article.getDeleteState()==1||article.getState()==1){
            log.error("帖子信息异常");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //为回复设置初始值
        reply.setState((byte)0);
        reply.setDeleteState((byte)0);
        reply.setLikeCount(0);
        Date date = new Date();
        reply.setCreateTime(date);
        reply.setUpdateTime(date);
        //将回复写进库中
        int result = articleReplyMapper.insertSelective(reply);
        if(result!=1){
            log.error("文章更新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //修改帖子对应的信息
        Article update = new Article();
        update.setReplyCount(article.getReplyCount()+1);
        update.setUpdateTime(new Date());
        update.setId(reply.getArticleId());
        //将修改的帖子信息写到库中
        int resultUpdate = articleMapper.updateByPrimaryKeySelective(update);
        if(resultUpdate!=1){
            log.error("文章信息修改失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_UPDATE));
        }
        //输出成功信息
        log.info("文章信息更新成功，reply(文章回复信息)为"+reply);
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //验证articleId的有效性
        if(articleId==null){
            log.error("articleId为空");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //验证对应文章信息的有效性
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article==null||article.getState()==1||article.getDeleteState()==1){
            log.error("帖子参数错误");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用对应api返回文章信息
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);
        log.info("文章回复信息显示成功");
        return articleReplies;
    }
}
